//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ManipulateDistribution.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Distribution.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (boost::shared_ptr<QuantLib::ManipulateDistribution>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phManipulateDistribution = NULL;
#endif
	_ppManipulateDistribution = &childNative;
    
}
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (QuantLib::ManipulateDistribution& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phManipulateDistribution = NULL;
#endif
	_ppManipulateDistribution = new boost::shared_ptr<QuantLib::ManipulateDistribution> (&childNative);
    
    _ManipulateDistributionOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (CManipulateDistribution^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phManipulateDistribution = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppManipulateDistribution = new boost::shared_ptr<QuantLib::ManipulateDistribution> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phManipulateDistribution = NULL;
#endif
	if (!t->IsSubclassOf(CManipulateDistribution::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (QuantLib::Handle<QuantLib::ManipulateDistribution>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phManipulateDistribution = &childNative;
	_ppManipulateDistribution = &static_cast<boost::shared_ptr<QuantLib::ManipulateDistribution>>(childNative.currentLink());
    
    _ManipulateDistributionOwner = owner;
}
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (QuantLib::Handle<QuantLib::ManipulateDistribution> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phManipulateDistribution = &childNative;
	_ppManipulateDistribution = &static_cast<boost::shared_ptr<QuantLib::ManipulateDistribution>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CManipulateDistribution::CManipulateDistribution (QuantLib::ManipulateDistribution childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phManipulateDistribution = NULL;
#endif
	_ppManipulateDistribution = new boost::shared_ptr<QuantLib::ManipulateDistribution> (new QuantLib::ManipulateDistribution (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CManipulateDistribution::~CManipulateDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppManipulateDistribution != NULL)
    {
	    delete _ppManipulateDistribution;
        _ppManipulateDistribution = NULL;
    }
}
Cephei::QL::Experimental::Credit::CManipulateDistribution::!CManipulateDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppManipulateDistribution != NULL)
    {
	    delete _ppManipulateDistribution;
    }
}
QuantLib::ManipulateDistribution& Cephei::QL::Experimental::Credit::CManipulateDistribution::GetReference ()
{
    if (_ppManipulateDistribution == NULL) throw REFNEW NativeNullException ();
	return **_ppManipulateDistribution;
}
boost::shared_ptr<QuantLib::ManipulateDistribution>& Cephei::QL::Experimental::Credit::CManipulateDistribution::GetShared ()
{
    if (_ppManipulateDistribution == NULL) throw REFNEW NativeNullException ();
	return *_ppManipulateDistribution;
}
QuantLib::ManipulateDistribution* Cephei::QL::Experimental::Credit::CManipulateDistribution::GetPointer ()
{
    if (_ppManipulateDistribution == NULL) throw REFNEW NativeNullException ();
	return &**_ppManipulateDistribution;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ManipulateDistribution>& Cephei::QL::Experimental::Credit::CManipulateDistribution::GetHandle ()
{
	if (_phManipulateDistribution == NULL)
	{
		_phManipulateDistribution = new Handle<QuantLib::ManipulateDistribution> (*_ppManipulateDistribution);
	}
	return *_phManipulateDistribution;
}
#endif
bool Cephei::QL::Experimental::Credit::CManipulateDistribution::HasNative () 
{
	return (_ppManipulateDistribution != NULL);
}

Cephei::QL::Experimental::Credit::IDistribution^ Cephei::QL::Experimental::Credit::CManipulateDistribution::Convolve (Cephei::QL::Experimental::Credit::IDistribution^ d1, Cephei::QL::Experimental::Credit::IDistribution^ d2)
{
    CDistribution^ _Cd1;
    CDistribution^ _Cd2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cd1 = safe_cast<CDistribution^> (d1);
        _Cd1->Lock();
        QuantLib::Distribution& _d1 = static_cast<QuantLib::Distribution&> (_Cd1->GetReference ()); 
        _Cd2 = safe_cast<CDistribution^> (d2);
        _Cd2->Lock();
        QuantLib::Distribution& _d2 = static_cast<QuantLib::Distribution&> (_Cd2->GetReference ()); 
    	QuantLib::Distribution _rv = (QuantLib::Distribution)(*_ppManipulateDistribution)->convolve ( _d1,  _d2 );   
        Cephei::QL::Experimental::Credit::CDistribution^ _nrv = REFNEW Cephei::QL::Experimental::Credit::CDistribution (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cd1 != nullptr) _Cd1->Unlock();
        if (_Cd2 != nullptr) _Cd2->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

